package com.systemframework.pay.config;

import cn.hutool.core.util.StrUtil;
import com.github.binarywang.wxpay.config.WxPayConfig;
import com.github.binarywang.wxpay.service.WxPayService;
import com.github.binarywang.wxpay.service.impl.WxPayServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author summer
 */
@Configuration
public class WxPayConfiguration {
  private WxPayProperties properties;

  @Autowired
  public void setProperties(WxPayProperties properties) {
    this.properties = properties;
  }

  @Bean
  public WxPayService wxService() {
    WxPayConfig payConfig = new WxPayConfig();
    payConfig.setAppId(this.properties.getAppId());
    payConfig.setMchId(this.properties.getMchId());
    payConfig.setMchKey(this.properties.getMchKey());
    payConfig.setKeyPath(this.properties.getKeyPath());
    WxPayService wxPayService = new WxPayServiceImpl();
    if (StrUtil.isNotEmpty(properties.getAppId())){
      wxPayService.setConfig(payConfig);
    }

    return wxPayService;
  }

}
